package top.xbaoziplus.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xbaoziplus.company.common.CustomException;
import top.xbaoziplus.company.empty.*;
import top.xbaoziplus.company.mapper.StaffMapper;
import top.xbaoziplus.company.service.AppointmentService;
import top.xbaoziplus.company.service.PositionService;
import top.xbaoziplus.company.service.SalaryService;
import top.xbaoziplus.company.service.StaffService;

import java.sql.SQLException;

/**
 *
 */
@Service
@Slf4j
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff>
    implements StaffService{
    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private PositionService positionService;

    @Override
    public void save(UserInfo userInfo) {
        // 设置员工数据
        Staff staff = new Staff();
        BeanUtils.copyProperties(userInfo, staff);
        try {
            // 保存员工信息
            this.save(staff);
            // 获取员工信息
            LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Staff::getName, staff.getName());
            queryWrapper.orderByDesc(Staff::getCreateTime);
            queryWrapper.last("limit 1");
            Staff staffInfo = staffMapper.selectOne(queryWrapper);
            // 设置员工工资
            Salary salary = new Salary().setJobId(staffInfo.getJobId()).setSalary(userInfo.getSalary());
            // 保存员工工资
            salaryService.save(salary);
            Position position = positionService.getById(userInfo.getPositionId());
            // 设置员工职位
            Appointment appointment = new Appointment()
                    .setJobId(staffInfo.getJobId())
                    .setPositionId(userInfo.getPositionId())
                    .setPositionName(position.getName());
            // 保存员工职位
            appointmentService.save(appointment);
        } catch (NullPointerException e) {
            throw new CustomException("请确保员工数据的完整性和正确性后新增");
        }
    }

    @Override
    public void update(UserInfo userInfo) {
        // 设置员工数据
        Staff staff = new Staff();
        BeanUtils.copyProperties(userInfo, staff);
        try {
            // 更新员工信息
            this.updateById(staff);

            // 更新员工职位
            Position position = positionService.getById(userInfo.getPositionId());
            LambdaUpdateWrapper<Appointment> appointmentWrapper = new LambdaUpdateWrapper<>();
            appointmentWrapper.eq(Appointment::getJobId, staff.getJobId());
            appointmentWrapper.set(Appointment::getPositionId, position.getId());
            appointmentWrapper.set(Appointment::getPositionName, position.getName());
            appointmentService.update(appointmentWrapper);
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new CustomException("请确保员工数据的完整性和正确性后更新");
        } catch (Exception e) {
            throw new CustomException("更新失败");
        }
    }
}




